<template>
	<view class="xydl">
	  <view class="all">
		 <div>幸运大礼</div>
		 <div>幸运值</div>
		 <div class="xingyunzhi">{{xingyunzhi}}</div>
		 <button class="xuanzhi" @click="xuanzhi" type="warn">抽取幸运值</button>
		 <button class="lingqujiangli" @click="lingqujiangli" type="primary">领取奖励</button>
	  </view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				xingyunzhi:0,
				zhang:"",
				xingyundalicishu:0,
				ab:-1
			}
		},
		created(){
			this.zhang=getApp().globalData.zhang
			 let now = new Date()
			   let nian=now.getFullYear()
			   let yue =now.getMonth()+1
			   let ri =now.getDate()
			   	
			   uni.request({
			   	url:"http://124.221.104.5:8080/huodong/update",
			   	data:{zhang:this.zhang,yue:yue,ri:ri,nian:nian},
			   success: (res) => {
			   	this.xingyunzhi=res.data.xingyunzhi
			   }
			   });
		},
		methods: {
		   async xuanzhi(){
			  
			   uni.request({
			   	url:"http://124.221.104.5:8080/huodong/xingyundali",
				data:{zhang:this.zhang},
				success:(res) => {
					this.ab=res.data
				          },
						 
			   });
			    await new Promise((reslove)=>setTimeout(reslove,100))
			   if(this.ab>-1)
			   {
			   	 	for(let z =1;z<=35;z++)
			             { 
			   			  this.xingyunzhi=Math.floor(Math.random()*100+1)
			            await new Promise((reslove)=>setTimeout(reslove,60))
			              }
						   await new Promise((reslove)=>setTimeout(reslove,60))
			             this.xingyunzhi=this.ab
			   }
			   else if(this.ab==-1)
				   
			   {
				  uni.showToast({
				  	title:"你今天已经抽取过了哦!",
					icon:'error',
					duration:2000
				  })
			   }
			  
		   	},
			lingqujiangli(){
				if(this.ab!=-1)
				{uni.request({
					url:"http://124.221.104.5:8080/jbgn/lingjindou",
					data:{zhang:this.zhang,jindou:this.xingyunzhi*100}
				})
				uni.showToast({
					title:"恭喜你获得"+this.xingyunzhi*100+"豆",
					duration:2000,
					icon:"none"
				})
				}
				else{
					uni.showToast({
						title:"已经领取过了哦!",
						duration:2000,
						icon:"error"
					})
				}
			}
			
			
		}
	}
</script>

<style>
        .xydl{
			width: 420rpx;
			height: 320rpx;
			background-color: bisque;
			position: absolute;
			left: 50%;
			top: 50%;
			transform: translate(-50%,-50%);
		}
		.all{
			display: flex;
			flex-direction: column;
			align-items: center;
		}
		.xingyunzhi{
			margin-top: 40rpx;
			font-size: 40rpx;
			
		}
		.xuanzhi{
			margin-top: 40rpx;
		}
		.lingqujiangli{
			margin-top: 40rpx;
		}
</style>
